package com.newangels.action;

import com.newangels.service.AgentManagerService;
import com.newangels.service.impl.AgentManagerServiceImpl;
import net.sf.json.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 智能体管理页面数据
 *
 * @author: TangLiang
 * @date: 2020/5/17 17:11
 * @since: 1.0
 */
@WebServlet("/agentManage")
public class AgentManageServlet extends HttpServlet {

    AgentManagerService agentManagerService = AgentManagerServiceImpl.getAgentManagerServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/plain");
        //获取传递的参数
        Integer page = request.getParameter("page") == null ? -1 : Integer.parseInt(request.getParameter("page"));
        Integer limit = request.getParameter("limit") == null ? -1 : Integer.parseInt(request.getParameter("limit"));
        String AGENT_ID_ = request.getParameter("AGENT_ID_");
        String AGENT_CODE_ = request.getParameter("AGENT_CODE_");
        String AGENT_NAME_ = request.getParameter("AGENT_NAME_");
        String AGENT_URL_ = request.getParameter("AGENT_URL_");
        String AGENT_ARGUMENTS_ = request.getParameter("AGENT_ARGUMENTS_");
        String AGENT_STATE_ = request.getParameter("AGENT_STATE_");
        String STATE_ = request.getParameter("STATE_");
        Integer AGENT_COST_ = request.getParameter("AGENT_COST_") == null || request.getParameter("AGENT_COST_").length() == 0 ? null : Integer.parseInt(request.getParameter("AGENT_COST_"));
        String AGENT_SOURCE_ = request.getParameter("AGENT_SOURCE_");
        //查询数据
        List<Map<String, Object>> list = agentManagerService.selectAgents(AGENT_ID_, AGENT_CODE_, AGENT_NAME_, AGENT_URL_, AGENT_ARGUMENTS_, AGENT_STATE_, STATE_, AGENT_COST_, AGENT_SOURCE_, page, limit);
        int total = agentManagerService.getcount(AGENT_ID_, AGENT_CODE_, AGENT_NAME_, AGENT_URL_, AGENT_ARGUMENTS_, AGENT_STATE_, STATE_, AGENT_COST_, AGENT_SOURCE_);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", list);
        jsonObject.put("total", total);
        jsonObject.put("success", true);
        response.getWriter().write(jsonObject.toString());
    }

}
